/* Class: UWA.Environments.Frame

_Copyright 2006-2007 Netvibes. All rights reserved._

*/

UWA.extend( UWA.Environment.prototype, {
  
  initialize: function() {
    this.netvibes = {};
  },
  
  onRegisterModule: function(module) {
    this.html['body'] = $('moduleContent');
    this.module.elements['body'] = UWA.$element(this.html['body']);
    this.module.body = this.module.elements['body']; // shortcut
    this.setPeriodical('handleResizePeriodical', this.handleResize, 250);
    // note that we don't call launchModule like in many environments
  },

  /**
   * Return the variable value from the Environnement
   *
   * @public
   * @param {name}
   * @return mix
   */   
  getData: function(name) {
    if (this.data[name]) return this.data[name];
    return undefined;
  },
  
  /**
   * Set the variable in the Environnement
   *
   * @public
   * @param {name}
   * @param {value}
   * @return void
   */   
  setData: function(name, value) {
    if (this.data[name] != value) {
      this.data[name] = value;
      this.sendRemote('setValue', name, value);
    }
  },
  
  /* to document */
  setUnreadCount: function(count) {
    this.sendRemote('setUnreadCount', false, count);
  },
  
  /* to document */
  onSearch: function(query) {
    this.query = query;
  },
  
  /* to document */
  handleResize: function() {
    if(typeof this.module.body == "undefined") {
      UWA.log('widget.body is not defined : widget #' + this.widget.id + '');
      UWA.log(this.widget.body);
      return;
    }
    var height = parseInt(this.module.body.getDimensions().height);
    if(height > 0 && height != this.height) {
      this.sendRemote('resizeHeight', false, height);
      this.height = height;
    }
  },

  /* to document */
  onUpdateBody: function() {
    this.setDelayed('handleLinks', this.handleLinks, 100);
  },
  
  /* to document */
  setTitle: function(title) {
    this.sendRemote('setTitle', false, title);
  },
  
  /* to document */
  sendRemote: function(action, name, value) {
    try { var test = this.widget.widgetObject.communicationType } catch(e) {
      this.widget.log('widgetObject not ready : widget #' + this.widget.id + ' - ' + action + ':' + name + ':' + value);
      return;
    }
    // Call the public interface of the panel
    switch (this.widget.widgetObject.communicationType) {
	    case "TUAcallback":
	    	this.widget.widgetObject.publicInterface(action, name, value);
	    	break;
	    case "TUApolling":
	    case "TMUpolling":
        var commandString = UWA.Json.encode({ 'id' : this.module.id, 'action': action, 'name': name, 'value' : value });
	    	this.widget.widgetObject.poll.push(commandString);
	    	break;
    }
  },
  
  /* to document */
  publicInterface: function(action, name, value) {
  	if(action) {
      switch(action) {
        case 'onResize':
          this.widget.callback('onResize');
          this.widget.callback('onUpdateBody');
          break;
        case 'onRefresh':
          if(this.widget.onRefresh) this.widget.callback('onRefresh');
          else if(this.widget.onLoad) this.widget.callback('onLoad');
          break;
        case 'launchModule':
          this.launchModule();
          break;
        case 'setValue':
          this.widget.data[name] = value;
          break;
      }
    }
  },
  
  /*
  createTUAframe : function(widgetDomain, subspaceDomain) {
    // Create the access frame
  	var accessUrl = "http://" + widgetDomain + "/api/uwa/frame/access_tua.html";
  	accessUrl += "?id=" + this.widget.id + "&subspace=" + subspaceDomain;
  	var accessFrame = document.createElement("iframe");
  	accessFrame.setStyle("display", "none");
  	accessFrame.setAttribute("src", accessUrl);
  	document.body.appendChild(accessFrame);
  },
  */
  
  initPoll : function() {
		this.widget.poll = new Array();
		this.setPeriodical('poll', function() {
      var nextAction = this.widget.poll[0];
      var nextActionObject = UWA.Json.decode(nextAction);
	    if (nextAction) {
	      this.publicInterface(nextActionObject.action, nextActionObject.name, nextActionObject.value);
	      this.widget.poll.shift();
	    }
	  }, 250);
  },
  
  initTUAcallback : function(widgetDomain, subspaceDomain) {
    // this.createTUAframe(widgetDomain, subspaceDomain);
  },
  
  initTUApolling : function(widgetDomain, subspaceDomain) {
    // this.createTUAframe(widgetDomain, subspaceDomain);
    // Create polling process
    this.initPoll();
  },
  
  initTMUpolling : function(widgetDomain, subspaceDomain) {
    // Change the domain
    document.domain = subspaceDomain;
    // Register
    this.widget.widgetObject = parent.widgetObject;
    this.widget.widgetObject.widget = this.widget;
    // Create polling process
    this.initPoll();
    // Notify
    this.widget.widgetObject.poll.push('{"action":"widgetIsReady"}');
  }
  
} );

var AudioPlayer = {
  play: function(url, title) {
    Environment.sendRemote('audioPlayer', 'play', url);
  },
  pause: function() {
    Environment.sendRemote('audioPlayer', 'pause');
  },
  stop: function() {
    Environment.sendRemote('audioPlayer', 'stop');
  }
};